package com.jpsl.core.reflection.property;

import java.util.*;

/**
 *
 * @author 懒王猫
 */
public abstract class MetaClassSupport implements MetaClass {

    protected final Class<?> beanType;

    private final Map<String, String> properties;

    private final Map<String, PropertyValueGetter> getters;

    private final Map<String, PropertyValueSetter> setters;

    public MetaClassSupport(Class<?> beanType) {
        this.beanType = beanType;
        this.properties = new HashMap<>();
        this.getters = new HashMap<>();
        this.setters = new HashMap<>();
        init();
    }

    protected abstract void init();

    protected void addProperty(String property) {
        properties.putIfAbsent(property, property);
    }

    protected void addGetter(PropertyValueGetter getter) {
        getters.put(getter.getName(), getter);
    }

    protected void addSetter(PropertyValueSetter setter) {
        setters.put(setter.getName(), setter);
    }

    @Override
    public String getName() {
        return beanType.getName();
    }

    @Override
    public Class<?> getType() {
        return beanType;
    }

    @Override
    public List<String> getProperties() {
        return new ArrayList<>(properties.keySet());
    }

    @Override
    public Optional<PropertyValueGetter> getGetter(String property) {
        return Optional.ofNullable(getters.get(property));
    }

    @Override
    public Optional<PropertyValueSetter> getSetter(String property) {
        return Optional.ofNullable(setters.get(property));
    }

}
